package com.sinkdream.cms.controller;

import com.sinkdream.cms.model.News_one;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.UUID;

@Controller
@RequestMapping("/VideoUpload")
public class videoUploadControler {
    @Value("${imgPath}")
    private String imgPath  ;//在配置文件配置的图片路径
    /**
     * 视频上传，文章内容上传
     * @param file
     * @param
     * @return
     * @throws Exception
     */
    @RequestMapping("/uploadVideo")
    @ResponseBody
    @CrossOrigin
    public int fileUpload(@RequestParam(value="file",required = false) MultipartFile file , @RequestParam("title_op") String title_op) throws Exception {
        String fileName=null;
        int flag = 0;
        StringBuilder sp = new StringBuilder();
            if (file.isEmpty()) {
                System.out.println("文件为空");
                flag= 2;
            } else {
                fileName = file.getOriginalFilename();
                String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);//文件后缀名
                String filename0 = UUID.randomUUID()+"."+suffix;//文件名
                String filenames = imgPath+filename0;//路径+文件名
                sp.append(filename0);
                System.out.println("视频："+filenames);
                file.transferTo(new File(filenames));//保存到本地
                flag= 3;//成功
            }

        System.out.println("视频地址："+sp);
        System.out.println("标题："+title_op);

        return flag;
    }

}
